﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Empresas_Cadastros__wf_wfBeneficiosVA : System.Web.UI.Page
{
    protected int idWfConfig = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["IdWfConfig"] != null)
                idWfConfig = Int32.Parse(Request.QueryString["IdWfConfig"].ToString());

            if (!IsPostBack)
            {
                ExibeParametros();
            }

            AtivaAjax();
            OcultaMsgPagina(true);
        }
        catch (Exception ex)
        {
            ExibirMensagem(true, ex.Message);
        }
    }

    #region Eventos

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        try
        {
            CarregarTudo();
            OcultaMsgPagina(true);
        }
        catch (Exception ex)
        {
            ExibirMensagem(true, ex.Message);
        }
    }

    #region Alerta
    protected void GridAlerta_DeleteCommand(object source, GridCommandEventArgs e)
    {
        try
        {
            int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

            WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
            wfParametro.Excluir();

            CarregarTudo();
        }
        catch (Exception ex)
        {
            ExibirMensagem(true, ex.Message);
        }
    }

    protected void GridAlerta_ItemCreated(object sender, GridItemEventArgs e)
    {
        try
        {
            if (e.Item is GridCommandItem)
            {
                int total = 0;

                if (e.Item.OwnerTableView.DataSourceCount != null)
                    total = e.Item.OwnerTableView.DataSourceCount;

                if (total >= 4)
                {
                    HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                    btnInsert.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            ExibirMensagem(true, ex.Message);
        }
    }
    #endregion

    #region Pendencia
    protected void GridPendencia_DeleteCommand(object source, GridCommandEventArgs e)
    {
        try
        {
            int id = int.Parse((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdWfConfigParametro"].ToString());

            WorkflowConfigParametro wfParametro = new WorkflowConfigParametro(id);
            wfParametro.Excluir();

            CarregarTudo();
        }
        catch (Exception ex)
        {
            ExibirMensagem(true, ex.Message);
        }
    }

    protected void GridPendencia_ItemCreated(object sender, GridItemEventArgs e)
    {
        try
        {
            if (e.Item is GridCommandItem)
            {
                int total = 0;

                if (e.Item.OwnerTableView.DataSourceCount != null)
                    total = e.Item.OwnerTableView.DataSourceCount;

                if (total >= 4)
                {
                    HtmlGenericControl btnInsert = e.Item.FindControl("dvGridInsert") as HtmlGenericControl;
                    btnInsert.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            ExibirMensagem(true, ex.Message);
        }
    }
    #endregion

    #endregion

    #region Metodos
    private void AtivaAjax()
    {
        try
        {
            //Pagamento Unico
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridtpPagUnicoA, RadAjaxLoadingPanel1);
            RadAjaxManager1.AjaxSettings.AddAjaxSetting(RadAjaxManager1, GridtpPagUnicoP, RadAjaxLoadingPanel1);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void ExibeParametros()
    {
        try
        {
            //3	Entrega Mensal

            dvPagUnico.Visible = false;

            WorkflowConfigData wfConfigData = new WorkflowConfigData();
            List<WorkflowConfigData> listWkConfigData = wfConfigData.SelectPor(idWfConfig);

            foreach (WorkflowConfigData item in listWkConfigData)
            {
                switch (item.TipoPeriodicidade.IdTipoPeriodicidade)
                {
                    case 3:
                        dvPagUnico.Visible = true;
                        CarregarPagUnicoAlerta(item.IdWfConfigData);
                        CarregarPagUnicoAviso(item.IdWfConfigData);
                        hdIdWfConfigData_tpPagUnico.Value = item.IdWfConfigData.ToString();
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void CarregarTudo()
    {
        try
        {
            //Entrega Mensal
            if (Convert.ToInt32(hdIdWfConfigData_tpPagUnico.Value) != 0)
            {
                CarregarPagUnicoAlerta(Convert.ToInt32(hdIdWfConfigData_tpPagUnico.Value));
                CarregarPagUnicoAviso(Convert.ToInt32(hdIdWfConfigData_tpPagUnico.Value));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private List<WorkflowConfigParametro> RetornaWfParametroAlerta(int id, string tipo)
    {
        try
        {
            WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
            List<WorkflowConfigParametro> listParametrosAlerta = wfParametro.SelectAlerta(id, tipo);

            return listParametrosAlerta;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private List<WorkflowConfigParametro> RetornaWfParametroAviso(int id, string tipo)
    {
        try
        {
            WorkflowConfigParametro wfParametro = new WorkflowConfigParametro();
            List<WorkflowConfigParametro> listParametrosAlerta = wfParametro.SelectAviso(id, tipo);

            return listParametrosAlerta;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void ExibirMensagem(bool _msgErro, string _textoMsg)
    {
        try
        {
            OcultaMsgPagina(false);
            if (_msgErro)
            {
                lblResultado.ForeColor = System.Drawing.Color.Red;
                lblResultado.Text = _textoMsg;
            }
            else
            {
                lblResultado.ForeColor = System.Drawing.Color.Black;
                lblResultado.Text = _textoMsg;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void OcultaMsgPagina(bool oculta)
    {
        trMensagem.Visible = !oculta;
    }

    #region Pagamento Unico
    private void CarregarPagUnicoAlerta(int idWfConfigData)
    {
        try
        {
            GridtpPagUnicoA.DataSource = RetornaWfParametroAlerta(idWfConfigData, "Composto");
            GridtpPagUnicoA.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void CarregarPagUnicoAviso(int idWfConfigData)
    {
        try
        {
            GridtpPagUnicoP.DataSource = RetornaWfParametroAviso(idWfConfigData, "Composto");
            GridtpPagUnicoP.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion
    
    #endregion
}
